package com.ds.admin.config;

import jakarta.validation.MessageInterpolator;
import org.hibernate.validator.internal.engine.MessageInterpolatorContext;

import java.util.Locale;

/**
 * @class: Varitor
 * @description: 自定义消息插值器 默认提示消息前加上字段名
 * @author: fs
 * @create: 2024/4/8 17:49
 **/

public class CustomMessageInterpolator implements MessageInterpolator {

	private final MessageInterpolator delegate;

	public CustomMessageInterpolator(MessageInterpolator delegate) {
		this.delegate = delegate;
	}

	@Override
	public String interpolate(String messageTemplate, Context context) {

		return delegate.interpolate(messageTemplate, context);
	}

	@Override
	public String interpolate(String messageTemplate, Context context, Locale locale) {
		if (context instanceof MessageInterpolatorContext interpolatorContext) {
			String filedName = interpolatorContext.getPropertyPath().toString();
			return filedName + " " + delegate.interpolate(messageTemplate, context, locale);
		}
		return delegate.interpolate(messageTemplate, context, locale);
	}

}
